package com.example.demo99.Controller;

import com.example.demo99.Entity.DTO.OrderDto;
import com.example.demo99.Entity.Order;
import com.example.demo99.Enums.OrderStatus;
import com.example.demo99.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/addRaw")
    public String addRawOrder(@RequestBody Order order) {
        orderService.saveOrder(order);
        // 例如：下完单后默认改为“待支付”
        orderService.updateOrderStatus(order.getId(), OrderStatus.PENDING_PAYMENT);

        return "添加了订单,并设置为待支付";
    }

    @PostMapping("/add")
    public String addOrder(@RequestBody OrderDto dto) {
        orderService.createOrderAndGrantPoints(dto);
        return "订单添加成功，积分已发放";
    }


    @GetMapping("/all")
    public List<Order> getAllOrders() {
        return orderService.getAllOrders();
    }
}
